package org.jeecg.modules.zixishi.service.impl;

import org.jeecg.modules.coupon.entity.Coupon;
import org.jeecg.modules.coupon.mapper.CouponMapper;
import org.jeecg.modules.zixishi.entity.Quyu;
import org.jeecg.modules.zixishi.entity.SwiperPic;
import org.jeecg.modules.zixishi.entity.Seat;
import org.jeecg.modules.zixishi.mapper.SwiperPicMapper;
import org.jeecg.modules.zixishi.mapper.SeatMapper;
import org.jeecg.modules.zixishi.mapper.QuyuMapper;
import org.jeecg.modules.zixishi.service.IQuyuService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 区域
 * @Author: jeecg-boot
 * @Date:   2024-05-19
 * @Version: V1.0
 */
@Service
public class QuyuServiceImpl extends ServiceImpl<QuyuMapper, Quyu> implements IQuyuService {

	@Autowired
	private QuyuMapper quyuMapper;
	@Autowired
	private SwiperPicMapper swiperPicMapper;
	@Autowired
	private SeatMapper seatMapper;
	@Autowired
	private CouponMapper couponMapper;
	@Override
	public List<Quyu> selectByMainId(String mainId) {
		return quyuMapper.selectByMainId(mainId);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(Quyu quyu, List<SwiperPic> swiperPicList,List<Seat> seatList,List<Coupon> couponList) {
		quyuMapper.insert(quyu);
		if(swiperPicList!=null && swiperPicList.size()>0) {
			for(SwiperPic entity:swiperPicList) {
				//外键设置
				entity.setSourceId(quyu.getId());
				swiperPicMapper.insert(entity);
			}
		}
		if(seatList!=null && seatList.size()>0) {
			for(Seat entity:seatList) {
				//外键设置
				entity.setQuyuId(quyu.getId());
				entity.setZixishiId(quyu.getZixishiId());
				seatMapper.insert(entity);
			}
		}
		if(couponList!=null && couponList.size()>0) {
			for(Coupon entity:couponList) {
				//外键设置
				entity.setQuyuId(quyu.getId());
				entity.setZixishiId(quyu.getZixishiId());
				couponMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(Quyu quyu,List<SwiperPic> swiperPicList,List<Seat> seatList,List<Coupon> couponList) {
		quyuMapper.updateById(quyu);

		//1.先删除子表数据
		swiperPicMapper.deleteByMainId(quyu.getId());
		seatMapper.deleteByMainId(quyu.getId());
		couponMapper.deleteByMainId(quyu.getId());
		//2.子表数据重新插入
		if(swiperPicList!=null && swiperPicList.size()>0) {
			for(SwiperPic entity:swiperPicList) {
				//外键设置
				entity.setSourceId(quyu.getId());
				swiperPicMapper.insert(entity);
			}
		}
		if(seatList!=null && seatList.size()>0) {
			for(Seat entity:seatList) {
				//外键设置
				entity.setQuyuId(quyu.getId());
				entity.setZixishiId(quyu.getZixishiId());
				seatMapper.insert(entity);
			}
		}
		if(couponList!=null && couponList.size()>0) {
			for(Coupon entity:couponList) {
				//外键设置
				entity.setQuyuId(quyu.getId());
				entity.setZixishiId(quyu.getZixishiId());
				couponMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		swiperPicMapper.deleteByMainId(id);
		seatMapper.deleteByMainId(id);
		couponMapper.deleteByMainId(id);
		quyuMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			swiperPicMapper.deleteByMainId(id.toString());
			seatMapper.deleteByMainId(id.toString());
			couponMapper.deleteByMainId(id.toString());
			quyuMapper.deleteById(id);
		}
	}

}
